package marvin.client.core;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import marvin.client.rtp.StreamReceiver;

public class SubscriptionClient {

	private int serverPort;
	private String serverAddress;

	private Socket clientSocket;
	private TReader reader;
	private TWriter writer;
		
	private StreamReceiver rcv;
	private Map<String, StreamReceiver> radioReceivers;

	public SubscriptionClient(int serverPort, String serverAddress) {
		super();
		this.serverPort = serverPort;
		this.serverAddress = serverAddress;
		radioReceivers = new HashMap<String, StreamReceiver>();
		
		try {
			clientSocket = new Socket(serverAddress, serverPort);
			
			reader = new TReader(clientSocket.getInputStream(), this);
			writer = new TWriter(clientSocket.getOutputStream());
			reader.start();
		} catch (java.net.ConnectException e){
			e.printStackTrace();
		}
		catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void sendId(String id){
		if(writer != null)
			writer.sendId(id);
	}
	
	public void disconnect() throws IOException{
		if(writer != null)
			writer.disconnect();
		if(rcv != null)
			rcv.disconnect();
		clientSocket.close();
	}
	
	public int getServerPort() {
		return serverPort;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	public String getServerAddress() {
		return serverAddress;
	}

	public void setServerAddress(String serverAddress) {
		this.serverAddress = serverAddress;
	}

	public void disconnectReceiver(){
		if(rcv != null){
			rcv.disconnect();
		}
	}

	public void createWebRadioReceiver(String url) {
		if(!radioReceivers.containsKey(url)){
			if(rcv != null)
				rcv.disconnect();
			
			rcv = new StreamReceiver(url);
			rcv.connectToWebRadio();
			rcv.createPlayer();
		} else {
			rcv = radioReceivers.get(url);
			rcv.resumePlaying();
		}
	}

	public void createFilePlayer(String filePath) {
		if(rcv != null)
			rcv.disconnect();
		
		rcv = new StreamReceiver(new File(filePath), this);
		rcv.connectFile();
		rcv.createPlayer();
	}

	public void stopPlaying() {
		if(rcv != null)
			rcv.stopPlaying();		
	}
	public void suspendPlaying() {
		if(rcv != null)
			rcv.suspendPlaying();		
	}
	public void resumePlaying() {
		if(rcv != null)
			rcv.resumePlaying();		
	}

	public void sendEndOfMedia() {
		writer.sendEndOfMedia();
	}

	public void bufferWebRadioReceiver(final String url) {
		
		if(!radioReceivers.containsKey(url)){
			Thread t = new Thread(new Runnable() {
				public void run() {
					StreamReceiver radioRcv = new StreamReceiver(url);
					radioReceivers.put(url, radioRcv);
					radioRcv.connectToWebRadio();
					radioRcv.createPlayer();
					radioRcv.suspendPlaying();
				}
			});
			t.start();
		}
	}
}
